﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using UnityEngine;

public class TableDatabase
{
    public int ID;
}

public class ConfigTable<TDataBase, T> : Singleton<T> where TDataBase : TableDatabase, new() where T : new()
{

    // id ， 数据条目
    Dictionary<int, TDataBase> _cache = new Dictionary<int, TDataBase>();

    protected void load(string tablePath)
    {


        MemoryStream tableStream;
#if UNITY_EDITOR
        //开发期间，直接读取Project/Config
        var srcPath = Application.dataPath + "/../" + tablePath;
        tableStream = new MemoryStream(File.ReadAllBytes(srcPath));

#else

        //发布之后，读Resource/Config
         var table = Resources.Load<TextAsset>("Config/RoleTable");
         tableStream = new MemoryStream(table.bytes);
#endif



        using (var reader = new StreamReader(tableStream, Encoding.GetEncoding("gb2312")))
        {
            //跳过第一行
            var fieldNameStr = reader.ReadLine();
            var fieldNameArray = fieldNameStr.Split(',');
            List<FieldInfo> allFieldInfo = new List<FieldInfo>();
            foreach (var fieldName in fieldNameArray)
            {

                allFieldInfo.Add(typeof(TDataBase).GetField(fieldName));
            }

            //正式数据
            var lineStr = reader.ReadLine();
            while (lineStr != null)
            {
                lineStr = readLine(reader, allFieldInfo, lineStr);
            }
            reader.Close();
            tableStream.Close();

        }
    }

    private string readLine(StreamReader reader, List<FieldInfo> allFieldInfo, string lineStr)
    {
        var itemStrArray = lineStr.Split(',');
        var roleDB = new TDataBase();

        for (int i = 0; i < allFieldInfo.Count; i++)
        {
            var field = allFieldInfo[i];
            var data = itemStrArray[i];

            if (field.FieldType == typeof(int))
            {
                field.SetValue(roleDB, int.Parse(data));
            }
            else if (field.FieldType == typeof(string))
            {
                field.SetValue(roleDB, data);
            }
            else if (field.FieldType == typeof(float))
            {
                field.SetValue(roleDB, float.Parse(data));
            }
            else if (field.FieldType == typeof(bool))
            {
                field.SetValue(roleDB, bool.Parse(data));
            }
            else if (field.FieldType == typeof(List<int>))
            {
                var list = new List<int>();
                foreach (var itemStr in data.Split('$'))
                {
                    list.Add(int.Parse(itemStr));
                }
                field.SetValue(roleDB, list);
            }
            else if (field.FieldType == typeof(List<string>))
            {
                var list = new List<string>(data.Split('$'));

                field.SetValue(roleDB, list);
            }
            else if (field.FieldType == typeof(List<float>))
            {
                var list = new List<float>();
                foreach (var itemStr in data.Split('$'))
                {
                    list.Add(float.Parse(itemStr));
                }
                field.SetValue(roleDB, list);
            }
        }
        _cache[roleDB.ID] = roleDB;
        lineStr = reader.ReadLine();
        return lineStr;
    }


    //获取表格数据 ---- 索引器
    public TDataBase this[int index]
    {
        get
        {
            TDataBase db;
            _cache.TryGetValue(index, out db);
            return db;
        }
    }

    public Dictionary<int, TDataBase> GetAll()
    {
        return _cache;
    }



}
